package com.fy56.platform.logistics.controller;

import com.alibaba.fastjson.JSON;
import com.fy56.platform.logistics.common.BaseController;
import com.fy56.platform.logistics.common.ResultModel;
import com.fy56.platform.logistics.model.Advertise;
import com.fy56.platform.logistics.model.User;
import com.fy56.platform.logistics.service.web.IAdvertiseService;
import com.fy56.platform.logistics.utils.LogUtils;
import com.fy56.platform.logistics.utils.SessionUtil;
import com.fy56.platform.logistics.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;

/**
 * Created by Ning on 2018/5/9.
 */
@RestController
@RequestMapping("/controller/advertise")
public class AdvertiseController extends BaseController {

    @Autowired
    private IAdvertiseService iAdvertiseService;

    @RequestMapping(value = "/get", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultModel> get(Long keyId){
        Advertise ad = new Advertise();
        if(null != keyId){
            ad = iAdvertiseService.selectByPrimaryKey(keyId);
        }
        ResultModel r = ResultModel.ok(ad);
        return  new ResponseEntity<>(r, HttpStatus.OK);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultModel> save(Advertise entity, HttpServletRequest request, HttpServletResponse response){
        ResultModel resultModel = null;
        try {
            if (entity != null) {
                User user = SessionUtil.getSessionUser(request);
                if(null == user){
                    throw new Exception();
                }
                entity.setCreateBy(user);
                entity.setCreateTime(new Date());
                //执行插入
                resultModel = iAdvertiseService.insertSelective(entity);
                //保存系统业务日志
                LogUtils.saveLog(request, "成功", entity.getId().toString(), null, "AdvertiseManage", "save", "添加", JSON.toJSONString(entity));
                return new ResponseEntity<ResultModel>(resultModel, HttpStatus.OK);
            }
            return new ResponseEntity<ResultModel>(new ResultModel(false), HttpStatus.OK);
        }catch (Exception e){
            //保存系统业务日志
            String key = (entity == null || entity.getId() == null) ? null : entity.getId().toString();
            LogUtils.saveLog(request, "失败", key, null, "AdvertiseManage", "save", "添加", JSON.toJSONString(entity));
            return new ResponseEntity<ResultModel>(new ResultModel(EXCEPTION_MSG), HttpStatus.OK);
        }finally {
            resultModel = null;
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultModel> update(Advertise entity, HttpServletRequest request,HttpServletResponse response ){
        ResultModel resultModel = null;
        try {
            if (entity != null && null != entity.getId()) {
                User user = SessionUtil.getSessionUser(request);
                if(null == user){
                    throw new Exception();
                }
                entity.setUpdateBy(user);
                entity.setUpdateTime(new Date());
                //执行更新
                resultModel = iAdvertiseService.updateSelective(entity);
                //保存系统业务日志
                LogUtils.saveLog(request, "成功", entity.getId().toString(), null, "AdvertiseManage", "update", "编辑", JSON.toJSONString(entity));
                return new ResponseEntity<ResultModel>(resultModel, HttpStatus.OK);
            }
            return new ResponseEntity<ResultModel>(new ResultModel(false), HttpStatus.OK);
        }catch (Exception e){
            //保存系统业务日志
            String key = (entity == null || entity.getId() == null) ? null : entity.getId().toString();
            LogUtils.saveLog(request, "失败", key, null, "AdvertiseManage", "update", "编辑", JSON.toJSONString(entity));
            return new ResponseEntity<ResultModel>(new ResultModel(EXCEPTION_MSG), HttpStatus.OK);
        }finally {
            resultModel = null;
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultModel> delete(Long keyId, HttpServletRequest request) {
        ResultModel resultModel = null;
        Advertise advertise = null;
        try{
            if (keyId != null && (keyId.intValue() !=0)) {
                advertise = new Advertise();
                advertise.setId(keyId);
                advertise.setUpdateBy(SessionUtil.getSessionUser(request));
                advertise.setUpdateTime(new Date());

                //执行删除操作
                resultModel = iAdvertiseService.delete(advertise);

                //保存系统业务日志
                LogUtils.saveLog(request,"成功",keyId.toString(),null,"AdvertiseManage","delete","禁用", keyId.toString());
                return new ResponseEntity<ResultModel>(resultModel, HttpStatus.OK);
            }
            return new ResponseEntity<ResultModel>(new ResultModel("广告禁用失败!"), HttpStatus.OK);
        }catch (Exception e){

            //保存系统业务日志
            LogUtils.saveLog(request,"失败",keyId.toString(),null,"AdvertiseManage","delete","禁用", keyId.toString());
            return new ResponseEntity<ResultModel>(new ResultModel(EXCEPTION_MSG), HttpStatus.OK);
        }finally {
            resultModel = null;
            advertise = null;
        }
    }

    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultModel> batchDelete(String keyIds, HttpServletRequest request) {
        try{
            if (StringUtil.isNotEmpty(keyIds)) {

                //构建删除对象（服务器对象的集合）
                LinkedList<Advertise> list = new LinkedList<Advertise>();
                Advertise advertise = null;
                for(String id : keyIds.split(",")){
                    if(StringUtil.isNotEmpty(id)){
                        advertise = new Advertise();
                        advertise.setId(Long.parseLong(id));
                        advertise.setUpdateBy(SessionUtil.getSessionUser(request));
                        advertise.setUpdateTime(new Date());
                        list.add(advertise);
                    }
                }

                //执行删除操作
                ResultModel resultModel = iAdvertiseService.batchDelete(new ArrayList<Advertise>(list));

                //保存系统业务日志1
                LogUtils.saveLog(request,"成功",keyIds,null,"AdvertiseManage","batchDelete","批量禁用",keyIds);
                return new ResponseEntity<ResultModel>(resultModel, HttpStatus.OK);
            }
            return new ResponseEntity<ResultModel>(new ResultModel("广告批量禁用失败!"), HttpStatus.OK);
        }catch (Exception e){
            //保存系统业务日志
            LogUtils.saveLog(request,"失败",keyIds,null,"AdvertiseManage","batchDelete","批量禁用", keyIds);
            return new ResponseEntity<ResultModel>(new ResultModel(EXCEPTION_MSG), HttpStatus.OK);
        }
    }

}
